﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<BugzX.Models.ViewBugViewData>" %>

<div id="divError"></div>

<p><b>Number:&nbsp;</b><span id="spanNumber"></span></p>

<b id="bugComponent"></b>

<p><b>Description:</b></p>
<p><textarea cols="30" rows="5" id="bugDescription"></textarea></p>

<p><b>Owner:&nbsp;</b><span id="bugOwner"></span></p>
<p><b>Stamp:&nbsp;</b><span id="bugStamp"></span></p>
<p><b>Type:</b></p>
<p><select id="listBugType"></select></p>
<p><b>Status:</b></p>
<p><select id="listBugStatus"></select></p>
<p><b>Parent To:</b></p>
<p><input type="text" id="textParentTo" /></p>
<p><b>Depends On:</b></p>
<p><input type="text" id="textDependsOn" /></p>

<script type="text/javascript">

    function setupViewBugLoggedIn() {

        var bugId = $("#formViewBug").data("bugid");

        $.getJSON("/services/Bugs.ashx",
            {
                action: "getbug",
                bugId: bugId
            },
            function (data) {

                var d = data[0];

                $("#spanNumber").html(d.bugId);
                $("#bugComponent").html(d.component);
                $("#bugDescription").html(d.description);
                $("#bugOwner").html(d.ownerName);
                $("#bugStamp").html(d.stamp);

                setupParentTo(d);
                setupDependsOn(d);

                setupBugTypes(d);
                setupBugStatuses(d);
            }
        );
    }

    function setupParentTo(bug) {

        var html = "";

        $.each(bug.parentTo, function (i, v) {

            html += v + ",";
        });

        html = html.substring(0, html.length - 1);

        $("#textParentTo").val(html);
    }

    function setupDependsOn(bug) {

        var html = "";

        $.each(bug.dependsOn, function (i, v) {

            html += v + ",";
        });

        html = html.substring(0, html.length - 1);

        $("#textDependsOn").val(html);
    }

    function setupBugTypes(bug) {

        $.getJSON("/services/Bugs.ashx",
            {
                action: "getbugtypes"
            },
            function (data) {

                var html = "";

                $.each(data, function (i, v) {

                    var selected = "";

                    if (v.Value == bug.type)
                        selected = "selected=\"selected\"";

                    html += "<option value=\"" + v.Value + "\" " + selected + ">" + v.Key + "</option>";
                });

                $("#listBugType").html(html);
            }
        );
    }

    function setupBugStatuses(bug) {

        $.getJSON("/services/Bugs.ashx",
            {
                action: "getbugstatuses"
            },
            function (data) {

                var html = "";

                $.each(data, function (i, v) {

                    var selected = "";

                    if (v.Value == bug.status)
                        selected = "selected=\"selected\"";

                    html += "<option value=\"" + v.Value + "\" " + selected + ">" + v.Key + "</option>";
                });

                $("#listBugStatus").html(html);
            }
        );
    }

    function updateBug() {

        var result = false;
        var bugId = $("#formViewBug").data("bugid");

        $.ajax({
            async: false,
            cache: false,
            contentType: "json",
            url: "/services/Bugs.ashx",
            data: {
                action: "updatebug",
                bugId: bugId,
                description: encodeURIComponent($("#bugDescription").val()),
                type: $("#listBugType").val(),
                status: $("#listBugStatus").val(),
                parentTo: encodeURIComponent($("#textParentTo").val()),
                dependsOn: encodeURIComponent($("#textDependsOn").val())
            },
            success: function (data) {

                result = data.result;

                if (data.result == true) {

                    $("#divError").removeClass("ui-state-error").html("");
                }
                else {

                    $("#divError").addClass("ui-state-error").html(data.error);
                }
            }
        });

        return result;
    }

</script>